{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Pandas的分层索引MultiIndex\n",
    "\n",
    "为什么要学习分层索引MultiIndex？\n",
    "* 分层索引：在一个轴向上拥有多个索引层级，可以表达更高维度数据的形式；\n",
    "* 可以更方便的进行数据筛选，如果有序则性能更好；\n",
    "* groupby等操作的结果，如果是多KEY，结果是分层索引，需要会使用\n",
    "* 一般不需要自己创建分层索引(MultiIndex有构造函数但一般不用)\n",
    "\n",
    "\n",
    "演示数据：百度、阿里巴巴、爱奇艺、京东四家公司的10天股票数据  \n",
    "数据来自：英为财经  \n",
    "https://cn.investing.com/\n",
    "\n",
    "本次演示提纲：  \n",
    "一、Series的分层索引MultiIndex  \n",
    "二、Series有多层索引怎样筛选数据？  \n",
    "三、DataFrame的多层索引MultiIndex  \n",
    "四、DataFrame有多层索引怎样筛选数据？  "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "import pandas as pd\n",
    "%matplotlib inline"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "ename": "NameError",
     "evalue": "name 'pd' is not defined",
     "output_type": "error",
     "traceback": [
      "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[0;31mNameError\u001b[0m                                 Traceback (most recent call last)",
      "\u001b[0;32m<ipython-input-1-574feeeb8bb0>\u001b[0m in \u001b[0;36m<module>\u001b[0;34m\u001b[0m\n\u001b[0;32m----> 1\u001b[0;31m \u001b[0mstocks\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mpd\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mread_excel\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m'./datas/stocks/互联网公司股票.xlsx'\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m",
      "\u001b[0;31mNameError\u001b[0m: name 'pd' is not defined"
     ]
    }
   ],
   "source": [
    "stocks = pd.read_excel('./datas/stocks/互联网公司股票.xlsx')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(12, 8)"
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "stocks.shape"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>日期</th>\n",
       "      <th>公司</th>\n",
       "      <th>收盘</th>\n",
       "      <th>开盘</th>\n",
       "      <th>高</th>\n",
       "      <th>低</th>\n",
       "      <th>交易量</th>\n",
       "      <th>涨跌幅</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <td>0</td>\n",
       "      <td>2019-10-03</td>\n",
       "      <td>BIDU</td>\n",
       "      <td>104.32</td>\n",
       "      <td>102.35</td>\n",
       "      <td>104.73</td>\n",
       "      <td>101.15</td>\n",
       "      <td>2.24</td>\n",
       "      <td>0.02</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>1</td>\n",
       "      <td>2019-10-02</td>\n",
       "      <td>BIDU</td>\n",
       "      <td>102.62</td>\n",
       "      <td>100.85</td>\n",
       "      <td>103.24</td>\n",
       "      <td>99.50</td>\n",
       "      <td>2.69</td>\n",
       "      <td>0.01</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>2</td>\n",
       "      <td>2019-10-01</td>\n",
       "      <td>BIDU</td>\n",
       "      <td>102.00</td>\n",
       "      <td>102.80</td>\n",
       "      <td>103.26</td>\n",
       "      <td>101.00</td>\n",
       "      <td>1.78</td>\n",
       "      <td>-0.01</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "           日期    公司      收盘      开盘       高       低   交易量   涨跌幅\n",
       "0  2019-10-03  BIDU  104.32  102.35  104.73  101.15  2.24  0.02\n",
       "1  2019-10-02  BIDU  102.62  100.85  103.24   99.50  2.69  0.01\n",
       "2  2019-10-01  BIDU  102.00  102.80  103.26  101.00  1.78 -0.01"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "stocks.head(3)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array(['BIDU', 'BABA', 'IQ', 'JD'], dtype=object)"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "stocks[\"公司\"].unique()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "RangeIndex(start=0, stop=12, step=1)"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "stocks.index"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "公司\n",
       "BABA    166.80\n",
       "BIDU    102.98\n",
       "IQ       15.90\n",
       "JD       28.35\n",
       "Name: 收盘, dtype: float64"
      ]
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "stocks.groupby('公司')[\"收盘\"].mean()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 一、Series的分层索引MultiIndex"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "公司    日期        \n",
       "BABA  2019-10-01    165.15\n",
       "      2019-10-02    165.77\n",
       "      2019-10-03    169.48\n",
       "BIDU  2019-10-01    102.00\n",
       "      2019-10-02    102.62\n",
       "      2019-10-03    104.32\n",
       "IQ    2019-10-01     15.92\n",
       "      2019-10-02     15.72\n",
       "      2019-10-03     16.06\n",
       "JD    2019-10-01     28.19\n",
       "      2019-10-02     28.06\n",
       "      2019-10-03     28.80\n",
       "Name: 收盘, dtype: float64"
      ]
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "ser = stocks.groupby(['公司', '日期'])['收盘'].mean()\n",
    "ser"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "多维索引中，空白的意思是：使用上面的值"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "MultiIndex([('BABA', '2019-10-01'),\n",
       "            ('BABA', '2019-10-02'),\n",
       "            ('BABA', '2019-10-03'),\n",
       "            ('BIDU', '2019-10-01'),\n",
       "            ('BIDU', '2019-10-02'),\n",
       "            ('BIDU', '2019-10-03'),\n",
       "            (  'IQ', '2019-10-01'),\n",
       "            (  'IQ', '2019-10-02'),\n",
       "            (  'IQ', '2019-10-03'),\n",
       "            (  'JD', '2019-10-01'),\n",
       "            (  'JD', '2019-10-02'),\n",
       "            (  'JD', '2019-10-03')],\n",
       "           names=['公司', '日期'])"
      ]
     },
     "execution_count": 9,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "ser.index"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th>日期</th>\n",
       "      <th>2019-10-01</th>\n",
       "      <th>2019-10-02</th>\n",
       "      <th>2019-10-03</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>公司</th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <td>BABA</td>\n",
       "      <td>165.15</td>\n",
       "      <td>165.77</td>\n",
       "      <td>169.48</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>BIDU</td>\n",
       "      <td>102.00</td>\n",
       "      <td>102.62</td>\n",
       "      <td>104.32</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>IQ</td>\n",
       "      <td>15.92</td>\n",
       "      <td>15.72</td>\n",
       "      <td>16.06</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>JD</td>\n",
       "      <td>28.19</td>\n",
       "      <td>28.06</td>\n",
       "      <td>28.80</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "日期    2019-10-01  2019-10-02  2019-10-03\n",
       "公司                                      \n",
       "BABA      165.15      165.77      169.48\n",
       "BIDU      102.00      102.62      104.32\n",
       "IQ         15.92       15.72       16.06\n",
       "JD         28.19       28.06       28.80"
      ]
     },
     "execution_count": 10,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# unstack把二级索引变成列\n",
    "ser.unstack()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "公司    日期        \n",
       "BABA  2019-10-01    165.15\n",
       "      2019-10-02    165.77\n",
       "      2019-10-03    169.48\n",
       "BIDU  2019-10-01    102.00\n",
       "      2019-10-02    102.62\n",
       "      2019-10-03    104.32\n",
       "IQ    2019-10-01     15.92\n",
       "      2019-10-02     15.72\n",
       "      2019-10-03     16.06\n",
       "JD    2019-10-01     28.19\n",
       "      2019-10-02     28.06\n",
       "      2019-10-03     28.80\n",
       "Name: 收盘, dtype: float64"
      ]
     },
     "execution_count": 11,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "ser"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>公司</th>\n",
       "      <th>日期</th>\n",
       "      <th>收盘</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <td>0</td>\n",
       "      <td>BABA</td>\n",
       "      <td>2019-10-01</td>\n",
       "      <td>165.15</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>1</td>\n",
       "      <td>BABA</td>\n",
       "      <td>2019-10-02</td>\n",
       "      <td>165.77</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>2</td>\n",
       "      <td>BABA</td>\n",
       "      <td>2019-10-03</td>\n",
       "      <td>169.48</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>3</td>\n",
       "      <td>BIDU</td>\n",
       "      <td>2019-10-01</td>\n",
       "      <td>102.00</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>4</td>\n",
       "      <td>BIDU</td>\n",
       "      <td>2019-10-02</td>\n",
       "      <td>102.62</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>5</td>\n",
       "      <td>BIDU</td>\n",
       "      <td>2019-10-03</td>\n",
       "      <td>104.32</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>6</td>\n",
       "      <td>IQ</td>\n",
       "      <td>2019-10-01</td>\n",
       "      <td>15.92</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>7</td>\n",
       "      <td>IQ</td>\n",
       "      <td>2019-10-02</td>\n",
       "      <td>15.72</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>8</td>\n",
       "      <td>IQ</td>\n",
       "      <td>2019-10-03</td>\n",
       "      <td>16.06</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>9</td>\n",
       "      <td>JD</td>\n",
       "      <td>2019-10-01</td>\n",
       "      <td>28.19</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>10</td>\n",
       "      <td>JD</td>\n",
       "      <td>2019-10-02</td>\n",
       "      <td>28.06</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>11</td>\n",
       "      <td>JD</td>\n",
       "      <td>2019-10-03</td>\n",
       "      <td>28.80</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "      公司          日期      收盘\n",
       "0   BABA  2019-10-01  165.15\n",
       "1   BABA  2019-10-02  165.77\n",
       "2   BABA  2019-10-03  169.48\n",
       "3   BIDU  2019-10-01  102.00\n",
       "4   BIDU  2019-10-02  102.62\n",
       "5   BIDU  2019-10-03  104.32\n",
       "6     IQ  2019-10-01   15.92\n",
       "7     IQ  2019-10-02   15.72\n",
       "8     IQ  2019-10-03   16.06\n",
       "9     JD  2019-10-01   28.19\n",
       "10    JD  2019-10-02   28.06\n",
       "11    JD  2019-10-03   28.80"
      ]
     },
     "execution_count": 12,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "ser.reset_index()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 二、Series有多层索引MultiIndex怎样筛选数据？"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "公司    日期        \n",
       "BABA  2019-10-01    165.15\n",
       "      2019-10-02    165.77\n",
       "      2019-10-03    169.48\n",
       "BIDU  2019-10-01    102.00\n",
       "      2019-10-02    102.62\n",
       "      2019-10-03    104.32\n",
       "IQ    2019-10-01     15.92\n",
       "      2019-10-02     15.72\n",
       "      2019-10-03     16.06\n",
       "JD    2019-10-01     28.19\n",
       "      2019-10-02     28.06\n",
       "      2019-10-03     28.80\n",
       "Name: 收盘, dtype: float64"
      ]
     },
     "execution_count": 13,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "ser"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {
    "scrolled": false
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "日期\n",
       "2019-10-01    102.00\n",
       "2019-10-02    102.62\n",
       "2019-10-03    104.32\n",
       "Name: 收盘, dtype: float64"
      ]
     },
     "execution_count": 14,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "ser.loc['BIDU']"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "102.62"
      ]
     },
     "execution_count": 15,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 多层索引，可以用元组的形式筛选\n",
    "ser.loc[('BIDU', '2019-10-02')]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "公司\n",
       "BABA    165.77\n",
       "BIDU    102.62\n",
       "IQ       15.72\n",
       "JD       28.06\n",
       "Name: 收盘, dtype: float64"
      ]
     },
     "execution_count": 16,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "ser.loc[:, '2019-10-02']"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 三、DataFrame的多层索引MultiIndex"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>日期</th>\n",
       "      <th>公司</th>\n",
       "      <th>收盘</th>\n",
       "      <th>开盘</th>\n",
       "      <th>高</th>\n",
       "      <th>低</th>\n",
       "      <th>交易量</th>\n",
       "      <th>涨跌幅</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <td>0</td>\n",
       "      <td>2019-10-03</td>\n",
       "      <td>BIDU</td>\n",
       "      <td>104.32</td>\n",
       "      <td>102.35</td>\n",
       "      <td>104.73</td>\n",
       "      <td>101.15</td>\n",
       "      <td>2.24</td>\n",
       "      <td>0.02</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>1</td>\n",
       "      <td>2019-10-02</td>\n",
       "      <td>BIDU</td>\n",
       "      <td>102.62</td>\n",
       "      <td>100.85</td>\n",
       "      <td>103.24</td>\n",
       "      <td>99.50</td>\n",
       "      <td>2.69</td>\n",
       "      <td>0.01</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>2</td>\n",
       "      <td>2019-10-01</td>\n",
       "      <td>BIDU</td>\n",
       "      <td>102.00</td>\n",
       "      <td>102.80</td>\n",
       "      <td>103.26</td>\n",
       "      <td>101.00</td>\n",
       "      <td>1.78</td>\n",
       "      <td>-0.01</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>3</td>\n",
       "      <td>2019-10-03</td>\n",
       "      <td>BABA</td>\n",
       "      <td>169.48</td>\n",
       "      <td>166.65</td>\n",
       "      <td>170.18</td>\n",
       "      <td>165.00</td>\n",
       "      <td>10.39</td>\n",
       "      <td>0.02</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>4</td>\n",
       "      <td>2019-10-02</td>\n",
       "      <td>BABA</td>\n",
       "      <td>165.77</td>\n",
       "      <td>162.82</td>\n",
       "      <td>166.88</td>\n",
       "      <td>161.90</td>\n",
       "      <td>11.60</td>\n",
       "      <td>0.00</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "           日期    公司      收盘      开盘       高       低    交易量   涨跌幅\n",
       "0  2019-10-03  BIDU  104.32  102.35  104.73  101.15   2.24  0.02\n",
       "1  2019-10-02  BIDU  102.62  100.85  103.24   99.50   2.69  0.01\n",
       "2  2019-10-01  BIDU  102.00  102.80  103.26  101.00   1.78 -0.01\n",
       "3  2019-10-03  BABA  169.48  166.65  170.18  165.00  10.39  0.02\n",
       "4  2019-10-02  BABA  165.77  162.82  166.88  161.90  11.60  0.00"
      ]
     },
     "execution_count": 17,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "stocks.head()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th>收盘</th>\n",
       "      <th>开盘</th>\n",
       "      <th>高</th>\n",
       "      <th>低</th>\n",
       "      <th>交易量</th>\n",
       "      <th>涨跌幅</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>公司</th>\n",
       "      <th>日期</th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <td rowspan=\"3\" valign=\"top\">BIDU</td>\n",
       "      <td>2019-10-03</td>\n",
       "      <td>104.32</td>\n",
       "      <td>102.35</td>\n",
       "      <td>104.73</td>\n",
       "      <td>101.15</td>\n",
       "      <td>2.24</td>\n",
       "      <td>0.02</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>2019-10-02</td>\n",
       "      <td>102.62</td>\n",
       "      <td>100.85</td>\n",
       "      <td>103.24</td>\n",
       "      <td>99.50</td>\n",
       "      <td>2.69</td>\n",
       "      <td>0.01</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>2019-10-01</td>\n",
       "      <td>102.00</td>\n",
       "      <td>102.80</td>\n",
       "      <td>103.26</td>\n",
       "      <td>101.00</td>\n",
       "      <td>1.78</td>\n",
       "      <td>-0.01</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td rowspan=\"3\" valign=\"top\">BABA</td>\n",
       "      <td>2019-10-03</td>\n",
       "      <td>169.48</td>\n",
       "      <td>166.65</td>\n",
       "      <td>170.18</td>\n",
       "      <td>165.00</td>\n",
       "      <td>10.39</td>\n",
       "      <td>0.02</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>2019-10-02</td>\n",
       "      <td>165.77</td>\n",
       "      <td>162.82</td>\n",
       "      <td>166.88</td>\n",
       "      <td>161.90</td>\n",
       "      <td>11.60</td>\n",
       "      <td>0.00</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>2019-10-01</td>\n",
       "      <td>165.15</td>\n",
       "      <td>168.01</td>\n",
       "      <td>168.23</td>\n",
       "      <td>163.64</td>\n",
       "      <td>14.19</td>\n",
       "      <td>-0.01</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td rowspan=\"3\" valign=\"top\">IQ</td>\n",
       "      <td>2019-10-03</td>\n",
       "      <td>16.06</td>\n",
       "      <td>15.71</td>\n",
       "      <td>16.38</td>\n",
       "      <td>15.32</td>\n",
       "      <td>10.08</td>\n",
       "      <td>0.02</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>2019-10-02</td>\n",
       "      <td>15.72</td>\n",
       "      <td>15.85</td>\n",
       "      <td>15.87</td>\n",
       "      <td>15.12</td>\n",
       "      <td>8.10</td>\n",
       "      <td>-0.01</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>2019-10-01</td>\n",
       "      <td>15.92</td>\n",
       "      <td>16.14</td>\n",
       "      <td>16.22</td>\n",
       "      <td>15.50</td>\n",
       "      <td>11.65</td>\n",
       "      <td>-0.01</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td rowspan=\"3\" valign=\"top\">JD</td>\n",
       "      <td>2019-10-03</td>\n",
       "      <td>28.80</td>\n",
       "      <td>28.11</td>\n",
       "      <td>28.97</td>\n",
       "      <td>27.82</td>\n",
       "      <td>8.77</td>\n",
       "      <td>0.03</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>2019-10-02</td>\n",
       "      <td>28.06</td>\n",
       "      <td>28.00</td>\n",
       "      <td>28.22</td>\n",
       "      <td>27.53</td>\n",
       "      <td>9.53</td>\n",
       "      <td>0.00</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>2019-10-01</td>\n",
       "      <td>28.19</td>\n",
       "      <td>28.22</td>\n",
       "      <td>28.57</td>\n",
       "      <td>27.97</td>\n",
       "      <td>10.64</td>\n",
       "      <td>0.00</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "                     收盘      开盘       高       低    交易量   涨跌幅\n",
       "公司   日期                                                     \n",
       "BIDU 2019-10-03  104.32  102.35  104.73  101.15   2.24  0.02\n",
       "     2019-10-02  102.62  100.85  103.24   99.50   2.69  0.01\n",
       "     2019-10-01  102.00  102.80  103.26  101.00   1.78 -0.01\n",
       "BABA 2019-10-03  169.48  166.65  170.18  165.00  10.39  0.02\n",
       "     2019-10-02  165.77  162.82  166.88  161.90  11.60  0.00\n",
       "     2019-10-01  165.15  168.01  168.23  163.64  14.19 -0.01\n",
       "IQ   2019-10-03   16.06   15.71   16.38   15.32  10.08  0.02\n",
       "     2019-10-02   15.72   15.85   15.87   15.12   8.10 -0.01\n",
       "     2019-10-01   15.92   16.14   16.22   15.50  11.65 -0.01\n",
       "JD   2019-10-03   28.80   28.11   28.97   27.82   8.77  0.03\n",
       "     2019-10-02   28.06   28.00   28.22   27.53   9.53  0.00\n",
       "     2019-10-01   28.19   28.22   28.57   27.97  10.64  0.00"
      ]
     },
     "execution_count": 18,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "stocks.set_index(['公司', '日期'], inplace=True)\n",
    "stocks"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "MultiIndex([('BIDU', '2019-10-03'),\n",
       "            ('BIDU', '2019-10-02'),\n",
       "            ('BIDU', '2019-10-01'),\n",
       "            ('BABA', '2019-10-03'),\n",
       "            ('BABA', '2019-10-02'),\n",
       "            ('BABA', '2019-10-01'),\n",
       "            (  'IQ', '2019-10-03'),\n",
       "            (  'IQ', '2019-10-02'),\n",
       "            (  'IQ', '2019-10-01'),\n",
       "            (  'JD', '2019-10-03'),\n",
       "            (  'JD', '2019-10-02'),\n",
       "            (  'JD', '2019-10-01')],\n",
       "           names=['公司', '日期'])"
      ]
     },
     "execution_count": 19,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "stocks.index"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th>收盘</th>\n",
       "      <th>开盘</th>\n",
       "      <th>高</th>\n",
       "      <th>低</th>\n",
       "      <th>交易量</th>\n",
       "      <th>涨跌幅</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>公司</th>\n",
       "      <th>日期</th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <td rowspan=\"3\" valign=\"top\">BABA</td>\n",
       "      <td>2019-10-01</td>\n",
       "      <td>165.15</td>\n",
       "      <td>168.01</td>\n",
       "      <td>168.23</td>\n",
       "      <td>163.64</td>\n",
       "      <td>14.19</td>\n",
       "      <td>-0.01</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>2019-10-02</td>\n",
       "      <td>165.77</td>\n",
       "      <td>162.82</td>\n",
       "      <td>166.88</td>\n",
       "      <td>161.90</td>\n",
       "      <td>11.60</td>\n",
       "      <td>0.00</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>2019-10-03</td>\n",
       "      <td>169.48</td>\n",
       "      <td>166.65</td>\n",
       "      <td>170.18</td>\n",
       "      <td>165.00</td>\n",
       "      <td>10.39</td>\n",
       "      <td>0.02</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td rowspan=\"3\" valign=\"top\">BIDU</td>\n",
       "      <td>2019-10-01</td>\n",
       "      <td>102.00</td>\n",
       "      <td>102.80</td>\n",
       "      <td>103.26</td>\n",
       "      <td>101.00</td>\n",
       "      <td>1.78</td>\n",
       "      <td>-0.01</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>2019-10-02</td>\n",
       "      <td>102.62</td>\n",
       "      <td>100.85</td>\n",
       "      <td>103.24</td>\n",
       "      <td>99.50</td>\n",
       "      <td>2.69</td>\n",
       "      <td>0.01</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>2019-10-03</td>\n",
       "      <td>104.32</td>\n",
       "      <td>102.35</td>\n",
       "      <td>104.73</td>\n",
       "      <td>101.15</td>\n",
       "      <td>2.24</td>\n",
       "      <td>0.02</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td rowspan=\"3\" valign=\"top\">IQ</td>\n",
       "      <td>2019-10-01</td>\n",
       "      <td>15.92</td>\n",
       "      <td>16.14</td>\n",
       "      <td>16.22</td>\n",
       "      <td>15.50</td>\n",
       "      <td>11.65</td>\n",
       "      <td>-0.01</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>2019-10-02</td>\n",
       "      <td>15.72</td>\n",
       "      <td>15.85</td>\n",
       "      <td>15.87</td>\n",
       "      <td>15.12</td>\n",
       "      <td>8.10</td>\n",
       "      <td>-0.01</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>2019-10-03</td>\n",
       "      <td>16.06</td>\n",
       "      <td>15.71</td>\n",
       "      <td>16.38</td>\n",
       "      <td>15.32</td>\n",
       "      <td>10.08</td>\n",
       "      <td>0.02</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td rowspan=\"3\" valign=\"top\">JD</td>\n",
       "      <td>2019-10-01</td>\n",
       "      <td>28.19</td>\n",
       "      <td>28.22</td>\n",
       "      <td>28.57</td>\n",
       "      <td>27.97</td>\n",
       "      <td>10.64</td>\n",
       "      <td>0.00</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>2019-10-02</td>\n",
       "      <td>28.06</td>\n",
       "      <td>28.00</td>\n",
       "      <td>28.22</td>\n",
       "      <td>27.53</td>\n",
       "      <td>9.53</td>\n",
       "      <td>0.00</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>2019-10-03</td>\n",
       "      <td>28.80</td>\n",
       "      <td>28.11</td>\n",
       "      <td>28.97</td>\n",
       "      <td>27.82</td>\n",
       "      <td>8.77</td>\n",
       "      <td>0.03</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "                     收盘      开盘       高       低    交易量   涨跌幅\n",
       "公司   日期                                                     \n",
       "BABA 2019-10-01  165.15  168.01  168.23  163.64  14.19 -0.01\n",
       "     2019-10-02  165.77  162.82  166.88  161.90  11.60  0.00\n",
       "     2019-10-03  169.48  166.65  170.18  165.00  10.39  0.02\n",
       "BIDU 2019-10-01  102.00  102.80  103.26  101.00   1.78 -0.01\n",
       "     2019-10-02  102.62  100.85  103.24   99.50   2.69  0.01\n",
       "     2019-10-03  104.32  102.35  104.73  101.15   2.24  0.02\n",
       "IQ   2019-10-01   15.92   16.14   16.22   15.50  11.65 -0.01\n",
       "     2019-10-02   15.72   15.85   15.87   15.12   8.10 -0.01\n",
       "     2019-10-03   16.06   15.71   16.38   15.32  10.08  0.02\n",
       "JD   2019-10-01   28.19   28.22   28.57   27.97  10.64  0.00\n",
       "     2019-10-02   28.06   28.00   28.22   27.53   9.53  0.00\n",
       "     2019-10-03   28.80   28.11   28.97   27.82   8.77  0.03"
      ]
     },
     "execution_count": 20,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "stocks.sort_index(inplace=True)\n",
    "stocks"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 四、DataFrame有多层索引MultiIndex怎样筛选数据？"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "【***重要知识***】在选择数据时： \n",
    "* 元组(key1,key2)代表筛选多层索引，其中key1是索引第一级，key2是第二级，比如key1=JD, key2=2019-10-02\n",
    "* 列表[key1,key2]代表同一层的多个KEY，其中key1和key2是并列的同级索引，比如key1=JD, key2=BIDU"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>收盘</th>\n",
       "      <th>开盘</th>\n",
       "      <th>高</th>\n",
       "      <th>低</th>\n",
       "      <th>交易量</th>\n",
       "      <th>涨跌幅</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>日期</th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <td>2019-10-01</td>\n",
       "      <td>102.00</td>\n",
       "      <td>102.80</td>\n",
       "      <td>103.26</td>\n",
       "      <td>101.00</td>\n",
       "      <td>1.78</td>\n",
       "      <td>-0.01</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>2019-10-02</td>\n",
       "      <td>102.62</td>\n",
       "      <td>100.85</td>\n",
       "      <td>103.24</td>\n",
       "      <td>99.50</td>\n",
       "      <td>2.69</td>\n",
       "      <td>0.01</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>2019-10-03</td>\n",
       "      <td>104.32</td>\n",
       "      <td>102.35</td>\n",
       "      <td>104.73</td>\n",
       "      <td>101.15</td>\n",
       "      <td>2.24</td>\n",
       "      <td>0.02</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "                收盘      开盘       高       低   交易量   涨跌幅\n",
       "日期                                                    \n",
       "2019-10-01  102.00  102.80  103.26  101.00  1.78 -0.01\n",
       "2019-10-02  102.62  100.85  103.24   99.50  2.69  0.01\n",
       "2019-10-03  104.32  102.35  104.73  101.15  2.24  0.02"
      ]
     },
     "execution_count": 21,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "stocks.loc['BIDU']"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "收盘     102.62\n",
       "开盘     100.85\n",
       "高      103.24\n",
       "低       99.50\n",
       "交易量      2.69\n",
       "涨跌幅      0.01\n",
       "Name: (BIDU, 2019-10-02), dtype: float64"
      ]
     },
     "execution_count": 22,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "stocks.loc[('BIDU', '2019-10-02'), :]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "100.85"
      ]
     },
     "execution_count": 23,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "stocks.loc[('BIDU', '2019-10-02'), '开盘']"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th>收盘</th>\n",
       "      <th>开盘</th>\n",
       "      <th>高</th>\n",
       "      <th>低</th>\n",
       "      <th>交易量</th>\n",
       "      <th>涨跌幅</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>公司</th>\n",
       "      <th>日期</th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <td rowspan=\"3\" valign=\"top\">BIDU</td>\n",
       "      <td>2019-10-01</td>\n",
       "      <td>102.00</td>\n",
       "      <td>102.80</td>\n",
       "      <td>103.26</td>\n",
       "      <td>101.00</td>\n",
       "      <td>1.78</td>\n",
       "      <td>-0.01</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>2019-10-02</td>\n",
       "      <td>102.62</td>\n",
       "      <td>100.85</td>\n",
       "      <td>103.24</td>\n",
       "      <td>99.50</td>\n",
       "      <td>2.69</td>\n",
       "      <td>0.01</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>2019-10-03</td>\n",
       "      <td>104.32</td>\n",
       "      <td>102.35</td>\n",
       "      <td>104.73</td>\n",
       "      <td>101.15</td>\n",
       "      <td>2.24</td>\n",
       "      <td>0.02</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td rowspan=\"3\" valign=\"top\">JD</td>\n",
       "      <td>2019-10-01</td>\n",
       "      <td>28.19</td>\n",
       "      <td>28.22</td>\n",
       "      <td>28.57</td>\n",
       "      <td>27.97</td>\n",
       "      <td>10.64</td>\n",
       "      <td>0.00</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>2019-10-02</td>\n",
       "      <td>28.06</td>\n",
       "      <td>28.00</td>\n",
       "      <td>28.22</td>\n",
       "      <td>27.53</td>\n",
       "      <td>9.53</td>\n",
       "      <td>0.00</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>2019-10-03</td>\n",
       "      <td>28.80</td>\n",
       "      <td>28.11</td>\n",
       "      <td>28.97</td>\n",
       "      <td>27.82</td>\n",
       "      <td>8.77</td>\n",
       "      <td>0.03</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "                     收盘      开盘       高       低    交易量   涨跌幅\n",
       "公司   日期                                                     \n",
       "BIDU 2019-10-01  102.00  102.80  103.26  101.00   1.78 -0.01\n",
       "     2019-10-02  102.62  100.85  103.24   99.50   2.69  0.01\n",
       "     2019-10-03  104.32  102.35  104.73  101.15   2.24  0.02\n",
       "JD   2019-10-01   28.19   28.22   28.57   27.97  10.64  0.00\n",
       "     2019-10-02   28.06   28.00   28.22   27.53   9.53  0.00\n",
       "     2019-10-03   28.80   28.11   28.97   27.82   8.77  0.03"
      ]
     },
     "execution_count": 24,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "stocks.loc[['BIDU', 'JD'], :]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 25,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th>收盘</th>\n",
       "      <th>开盘</th>\n",
       "      <th>高</th>\n",
       "      <th>低</th>\n",
       "      <th>交易量</th>\n",
       "      <th>涨跌幅</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>公司</th>\n",
       "      <th>日期</th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <td>BIDU</td>\n",
       "      <td>2019-10-03</td>\n",
       "      <td>104.32</td>\n",
       "      <td>102.35</td>\n",
       "      <td>104.73</td>\n",
       "      <td>101.15</td>\n",
       "      <td>2.24</td>\n",
       "      <td>0.02</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>JD</td>\n",
       "      <td>2019-10-03</td>\n",
       "      <td>28.80</td>\n",
       "      <td>28.11</td>\n",
       "      <td>28.97</td>\n",
       "      <td>27.82</td>\n",
       "      <td>8.77</td>\n",
       "      <td>0.03</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "                     收盘      开盘       高       低   交易量   涨跌幅\n",
       "公司   日期                                                    \n",
       "BIDU 2019-10-03  104.32  102.35  104.73  101.15  2.24  0.02\n",
       "JD   2019-10-03   28.80   28.11   28.97   27.82  8.77  0.03"
      ]
     },
     "execution_count": 25,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "stocks.loc[(['BIDU', 'JD'], '2019-10-03'), :]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 26,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "公司    日期        \n",
       "BIDU  2019-10-03    104.32\n",
       "JD    2019-10-03     28.80\n",
       "Name: 收盘, dtype: float64"
      ]
     },
     "execution_count": 26,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "stocks.loc[(['BIDU', 'JD'], '2019-10-03'), '收盘']"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 27,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "公司    日期        \n",
       "BIDU  2019-10-02    102.62\n",
       "      2019-10-03    104.32\n",
       "Name: 收盘, dtype: float64"
      ]
     },
     "execution_count": 27,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "stocks.loc[('BIDU', ['2019-10-02', '2019-10-03']), '收盘']"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 28,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th>收盘</th>\n",
       "      <th>开盘</th>\n",
       "      <th>高</th>\n",
       "      <th>低</th>\n",
       "      <th>交易量</th>\n",
       "      <th>涨跌幅</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>公司</th>\n",
       "      <th>日期</th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <td rowspan=\"2\" valign=\"top\">BABA</td>\n",
       "      <td>2019-10-02</td>\n",
       "      <td>165.77</td>\n",
       "      <td>162.82</td>\n",
       "      <td>166.88</td>\n",
       "      <td>161.90</td>\n",
       "      <td>11.60</td>\n",
       "      <td>0.00</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>2019-10-03</td>\n",
       "      <td>169.48</td>\n",
       "      <td>166.65</td>\n",
       "      <td>170.18</td>\n",
       "      <td>165.00</td>\n",
       "      <td>10.39</td>\n",
       "      <td>0.02</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td rowspan=\"2\" valign=\"top\">BIDU</td>\n",
       "      <td>2019-10-02</td>\n",
       "      <td>102.62</td>\n",
       "      <td>100.85</td>\n",
       "      <td>103.24</td>\n",
       "      <td>99.50</td>\n",
       "      <td>2.69</td>\n",
       "      <td>0.01</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>2019-10-03</td>\n",
       "      <td>104.32</td>\n",
       "      <td>102.35</td>\n",
       "      <td>104.73</td>\n",
       "      <td>101.15</td>\n",
       "      <td>2.24</td>\n",
       "      <td>0.02</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td rowspan=\"2\" valign=\"top\">IQ</td>\n",
       "      <td>2019-10-02</td>\n",
       "      <td>15.72</td>\n",
       "      <td>15.85</td>\n",
       "      <td>15.87</td>\n",
       "      <td>15.12</td>\n",
       "      <td>8.10</td>\n",
       "      <td>-0.01</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>2019-10-03</td>\n",
       "      <td>16.06</td>\n",
       "      <td>15.71</td>\n",
       "      <td>16.38</td>\n",
       "      <td>15.32</td>\n",
       "      <td>10.08</td>\n",
       "      <td>0.02</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td rowspan=\"2\" valign=\"top\">JD</td>\n",
       "      <td>2019-10-02</td>\n",
       "      <td>28.06</td>\n",
       "      <td>28.00</td>\n",
       "      <td>28.22</td>\n",
       "      <td>27.53</td>\n",
       "      <td>9.53</td>\n",
       "      <td>0.00</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>2019-10-03</td>\n",
       "      <td>28.80</td>\n",
       "      <td>28.11</td>\n",
       "      <td>28.97</td>\n",
       "      <td>27.82</td>\n",
       "      <td>8.77</td>\n",
       "      <td>0.03</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "                     收盘      开盘       高       低    交易量   涨跌幅\n",
       "公司   日期                                                     \n",
       "BABA 2019-10-02  165.77  162.82  166.88  161.90  11.60  0.00\n",
       "     2019-10-03  169.48  166.65  170.18  165.00  10.39  0.02\n",
       "BIDU 2019-10-02  102.62  100.85  103.24   99.50   2.69  0.01\n",
       "     2019-10-03  104.32  102.35  104.73  101.15   2.24  0.02\n",
       "IQ   2019-10-02   15.72   15.85   15.87   15.12   8.10 -0.01\n",
       "     2019-10-03   16.06   15.71   16.38   15.32  10.08  0.02\n",
       "JD   2019-10-02   28.06   28.00   28.22   27.53   9.53  0.00\n",
       "     2019-10-03   28.80   28.11   28.97   27.82   8.77  0.03"
      ]
     },
     "execution_count": 28,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# slice(None)代表筛选这一索引的所有内容\n",
    "stocks.loc[(slice(None), ['2019-10-02', '2019-10-03']), :]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 29,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>公司</th>\n",
       "      <th>日期</th>\n",
       "      <th>收盘</th>\n",
       "      <th>开盘</th>\n",
       "      <th>高</th>\n",
       "      <th>低</th>\n",
       "      <th>交易量</th>\n",
       "      <th>涨跌幅</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <td>0</td>\n",
       "      <td>BABA</td>\n",
       "      <td>2019-10-01</td>\n",
       "      <td>165.15</td>\n",
       "      <td>168.01</td>\n",
       "      <td>168.23</td>\n",
       "      <td>163.64</td>\n",
       "      <td>14.19</td>\n",
       "      <td>-0.01</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>1</td>\n",
       "      <td>BABA</td>\n",
       "      <td>2019-10-02</td>\n",
       "      <td>165.77</td>\n",
       "      <td>162.82</td>\n",
       "      <td>166.88</td>\n",
       "      <td>161.90</td>\n",
       "      <td>11.60</td>\n",
       "      <td>0.00</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>2</td>\n",
       "      <td>BABA</td>\n",
       "      <td>2019-10-03</td>\n",
       "      <td>169.48</td>\n",
       "      <td>166.65</td>\n",
       "      <td>170.18</td>\n",
       "      <td>165.00</td>\n",
       "      <td>10.39</td>\n",
       "      <td>0.02</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>3</td>\n",
       "      <td>BIDU</td>\n",
       "      <td>2019-10-01</td>\n",
       "      <td>102.00</td>\n",
       "      <td>102.80</td>\n",
       "      <td>103.26</td>\n",
       "      <td>101.00</td>\n",
       "      <td>1.78</td>\n",
       "      <td>-0.01</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>4</td>\n",
       "      <td>BIDU</td>\n",
       "      <td>2019-10-02</td>\n",
       "      <td>102.62</td>\n",
       "      <td>100.85</td>\n",
       "      <td>103.24</td>\n",
       "      <td>99.50</td>\n",
       "      <td>2.69</td>\n",
       "      <td>0.01</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>5</td>\n",
       "      <td>BIDU</td>\n",
       "      <td>2019-10-03</td>\n",
       "      <td>104.32</td>\n",
       "      <td>102.35</td>\n",
       "      <td>104.73</td>\n",
       "      <td>101.15</td>\n",
       "      <td>2.24</td>\n",
       "      <td>0.02</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>6</td>\n",
       "      <td>IQ</td>\n",
       "      <td>2019-10-01</td>\n",
       "      <td>15.92</td>\n",
       "      <td>16.14</td>\n",
       "      <td>16.22</td>\n",
       "      <td>15.50</td>\n",
       "      <td>11.65</td>\n",
       "      <td>-0.01</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>7</td>\n",
       "      <td>IQ</td>\n",
       "      <td>2019-10-02</td>\n",
       "      <td>15.72</td>\n",
       "      <td>15.85</td>\n",
       "      <td>15.87</td>\n",
       "      <td>15.12</td>\n",
       "      <td>8.10</td>\n",
       "      <td>-0.01</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>8</td>\n",
       "      <td>IQ</td>\n",
       "      <td>2019-10-03</td>\n",
       "      <td>16.06</td>\n",
       "      <td>15.71</td>\n",
       "      <td>16.38</td>\n",
       "      <td>15.32</td>\n",
       "      <td>10.08</td>\n",
       "      <td>0.02</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>9</td>\n",
       "      <td>JD</td>\n",
       "      <td>2019-10-01</td>\n",
       "      <td>28.19</td>\n",
       "      <td>28.22</td>\n",
       "      <td>28.57</td>\n",
       "      <td>27.97</td>\n",
       "      <td>10.64</td>\n",
       "      <td>0.00</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>10</td>\n",
       "      <td>JD</td>\n",
       "      <td>2019-10-02</td>\n",
       "      <td>28.06</td>\n",
       "      <td>28.00</td>\n",
       "      <td>28.22</td>\n",
       "      <td>27.53</td>\n",
       "      <td>9.53</td>\n",
       "      <td>0.00</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>11</td>\n",
       "      <td>JD</td>\n",
       "      <td>2019-10-03</td>\n",
       "      <td>28.80</td>\n",
       "      <td>28.11</td>\n",
       "      <td>28.97</td>\n",
       "      <td>27.82</td>\n",
       "      <td>8.77</td>\n",
       "      <td>0.03</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "      公司          日期      收盘      开盘       高       低    交易量   涨跌幅\n",
       "0   BABA  2019-10-01  165.15  168.01  168.23  163.64  14.19 -0.01\n",
       "1   BABA  2019-10-02  165.77  162.82  166.88  161.90  11.60  0.00\n",
       "2   BABA  2019-10-03  169.48  166.65  170.18  165.00  10.39  0.02\n",
       "3   BIDU  2019-10-01  102.00  102.80  103.26  101.00   1.78 -0.01\n",
       "4   BIDU  2019-10-02  102.62  100.85  103.24   99.50   2.69  0.01\n",
       "5   BIDU  2019-10-03  104.32  102.35  104.73  101.15   2.24  0.02\n",
       "6     IQ  2019-10-01   15.92   16.14   16.22   15.50  11.65 -0.01\n",
       "7     IQ  2019-10-02   15.72   15.85   15.87   15.12   8.10 -0.01\n",
       "8     IQ  2019-10-03   16.06   15.71   16.38   15.32  10.08  0.02\n",
       "9     JD  2019-10-01   28.19   28.22   28.57   27.97  10.64  0.00\n",
       "10    JD  2019-10-02   28.06   28.00   28.22   27.53   9.53  0.00\n",
       "11    JD  2019-10-03   28.80   28.11   28.97   27.82   8.77  0.03"
      ]
     },
     "execution_count": 29,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "stocks.reset_index()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.4"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
